﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Common;
/// <summary>
/// httpclien 工具类
/// </summary>
public class HttpHelper
{
    private readonly IHttpClientFactory _httpClientFactory;

    public HttpHelper(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }
    /// <summary>
    /// 初始化client对象
    /// </summary>
    /// <param name="token"></param>
    /// <returns></returns>
    private HttpClient GetClient(string? token ="")
    {
        var client = _httpClientFactory.CreateClient();
        //client.DefaultRequestHeaders.Add("Content-Type", "application/json; charset=utf-8");
        if (!string.IsNullOrWhiteSpace(token))
        {
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
        }

        client.Timeout = TimeSpan.FromSeconds(2);
        return client;
    }
    /// <summary>
    /// 获取content对象
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    private StringContent GetContent(object? data)
    {
        string content = "";
        if (data!=null)
        {
            content = JsonHelper.SerializeObject(data);
        }

        return new StringContent(content, Encoding.UTF8, "application/json");
    }
    /// <summary>
    /// 获取url
    /// </summary>
    /// <param name="originalUrl"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    private string GetUrl(string originalUrl, Dictionary<string, string>? parameters=null)
    {
        var url = originalUrl;
        if (parameters != null)
        {
            var strParam = string.Join("&", parameters.Select(o => o.Key + "=" + o.Value));
            url = string.Concat(url, '?', strParam);
        }

        return url;
    }
    /// <summary>
    /// 发送get请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    public async Task<TResult?> GetAsync<TResult>(string url, Dictionary<string, string>? parameters = null, string? token = "")
    {
        var client = GetClient(token);
        var address = GetUrl(url, parameters);
        var response = await client.GetAsync(address);
        return JsonHelper.DeserializeObject<TResult>(await response.Content.ReadAsStringAsync());
    }
    /// <summary>
    /// 发送post请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <param name="data"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    public async Task<TResult?> PostAsync<TResult>(string url, Dictionary<string, string>? parameters = null,
        object? data = null, string? token = "")
    {
        var client = GetClient( token);
        var address = GetUrl(url,parameters);
        var response = await client.PostAsync(address, GetContent(data));
        return JsonHelper.DeserializeObject<TResult>(await response.Content.ReadAsStringAsync());
    }

    /// <summary>
    /// 发送put请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <param name="data"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    public async Task<TResult?> PutAsync<TResult>(string url, Dictionary<string, string>? parameters = null,
        object? data = null, string? token = "")
    {
        var client = GetClient(token);
        var address = GetUrl(url, parameters);
        var response = await client.PutAsync(address, GetContent(data));
        return JsonHelper.DeserializeObject<TResult>(await response.Content.ReadAsStringAsync());
    }
    /// <summary>
    /// 发送delete请求
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="url"></param>
    /// <param name="parameters"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    public async Task<TResult?> DeleteAsync<TResult>(string url, Dictionary<string, string>? parameters = null,
        string? token = "")
    {
        // delete请求没有content对象
        var client = GetClient(token);
        var address = GetUrl(url, parameters);
        var response = await client.DeleteAsync(new Uri(address));
        return JsonHelper.DeserializeObject<TResult>(await response.Content.ReadAsStringAsync());
    }
}